package visao.cliente;

import visao.usuario.InterfaceLogin;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import modelo.dominio.Cliente;
import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaCliente;

/**
  * Essa Classe cria a Interface para alterar um Cliente
  * @author            Gustavo Arnaldo
  */



public class InterfaceAlterarCliente extends javax.swing.JFrame{
	
	private final PersistenciaCliente persistenciaCliente = new PersistenciaCliente();
	
	/*
	 * Construtor da Interface Alterar Cliente 
	 */
	public InterfaceAlterarCliente (){
		initComponents();
		
		this.setTitle("Alterações em Cliente");
		
		
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(true);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jLabel4.setVisible(false);
		jLabel5.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setVisible(true);
		jTextField2.setVisible(false);
		jTextField3.setVisible(false);
		jTextField4.setVisible(false);
		jTextField5.setVisible(false);
				
	}

	
     // <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Alterando Dados", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu Light", 0, 18), new java.awt.Color(76, 76, 76))); // NOI18N

        jButton3.setText("Confirmar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jButtonConfirmaActionPerformed(evt);
				} catch (IOException ex) {
					Logger.getLogger(InterfaceAlterarCliente.class.getName()).log(Level.SEVERE, null, ex);
				}
            }
        });

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel1.setText("CPF : ");

        jLabel2.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel2.setText("Nome :");

        jLabel3.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel3.setText("CPF : ");

        jLabel4.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel4.setText("Telefone : ");

        jLabel5.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel5.setText("Endereço : ");

        jButton5.setText("Ok");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jButtonOkActionPerformed(evt);
				} catch (IOException ex) {
					Logger.getLogger(InterfaceAlterarCliente.class.getName()).log(Level.SEVERE, null, ex);
				}
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                                    .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE))
                                .addGap(295, 295, 295)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(62, 62, 62)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(55, 55, 55)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addGap(23, 23, 23))
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoffActionPerformed(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 542, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(53, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>
	
	/*
	* Método do Botão Log Off que retorna para a Interface/Janela Login
	*	@param evt
	*/
	private void jButtonLogoffActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		ControleAcesso.logoff();
	} // Método do Log Off
	
	/*
	 * Método do Botão Voltar da Interface Alterar Cliente
	 * @param evt
	 */
	private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}// fim do Método Voltar
	
	/*
	 * Método Botão Cancelar que retornar a janela na forma incial
	 * @param evt
	 */
	private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(true);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel4.setVisible(false);
		jLabel5.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
		jTextField2.setText("");
		jTextField2.setVisible(false);
		jTextField4.setText("");
		jTextField4.setVisible(false);
		jTextField5.setText("");
		jTextField5.setVisible(false);
	}
	//Fim do Método Cancelar
	
	/*
	 * Método do botão Confirmar seta os campos alterados 
	 * e grava no Banco de Dados
	 * @param evt
	 * @throws IOException no caso de nenhum campo alterado
	 */
	private void jButtonConfirmaActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		long cpfPesquisa = Long.parseLong(jTextField1.getText());
		String nome = jTextField2.getText();
		String endereco = jTextField5.getText();
		String telefone = jTextField4.getText();
		Cliente novoCliente = new Cliente(cpfPesquisa);
		Cliente novoCliente2 = (Cliente) persistenciaCliente.consultar(novoCliente);
		
		if(nome.equals("") && endereco.equals("") && telefone.equals("")){
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Por favor, preencha pelo menos um campo!","Erro",JOptionPane.ERROR_MESSAGE);
		}else{
			if(!nome.equals("")){
				novoCliente2.setNome(nome);	
			}
			if(!endereco.equals("")){
				novoCliente2.setEndereco(endereco);	
			}
			if(!telefone.equals("")){
				novoCliente2.setTelefone(telefone);	
			}
			jButton1.setVisible(true);
			jButton2.setVisible(true);
			jButton3.setVisible(false);
			jButton4.setVisible(false);
			jButton5.setVisible(true);
			jLabel1.setVisible(true);
			jLabel2.setVisible(false);
			jLabel4.setVisible(false);
			jLabel5.setVisible(false);
			jPanel1.setVisible(true);
			jTextField1.setEnabled(true);
			jTextField1.setText("");
			jTextField1.setVisible(true);
			jTextField2.setText("");
			jTextField2.setVisible(false);
			jTextField4.setText("");
			jTextField4.setVisible(false);
			jTextField5.setText("");
			jTextField5.setVisible(false);
			persistenciaCliente.atualizar(novoCliente2);
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Alteração concluída!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	// Fim do Método Confirmar
	
	/**
	 * Método Botão Ok que procura um cliente no Banco de Dados
	 * @param evt
	 * @throws IOException 
	 */
	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		String nome = jTextField1.getText();
		
		if(!nome.equals("")){ 
			long cpfPesquisa = Long.parseLong(nome);
			Cliente novoCliente = new Cliente(cpfPesquisa);
			Cliente novoCliente2 = (Cliente) persistenciaCliente.consultar(novoCliente);
		
			if(novoCliente2 != null){
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(true);
				jButton4.setVisible(true);
				jButton5.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(true);
				jLabel4.setVisible(true);
				jLabel5.setVisible(true);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(false);
				jTextField1.setVisible(true);
				jTextField2.setVisible(true);
				jTextField4.setVisible(true);
				jTextField5.setVisible(true);	
			}else{
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não exite Cliente com esse CPF!","Erro",JOptionPane.ERROR_MESSAGE);
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(false);
				jButton4.setVisible(false);
				jButton5.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(false);
				jLabel4.setVisible(false);
				jLabel5.setVisible(false);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(true);
				jTextField1.setText("");
				jTextField1.setVisible(true);
				jTextField2.setText("");
				jTextField2.setVisible(false);
				jTextField4.setText("");
				jTextField4.setVisible(false);
				jTextField5.setText("");
				jTextField5.setVisible(false);
			}
		}else{
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digite um CPF por favor!","Erro",JOptionPane.ERROR_MESSAGE);
		}
	}
// Fiim do Método OK
	
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
}
// Fim da Classe Interface Alterar Cliente